package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.TopBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.EmptyUtils;
import com.example.hotel_lxy.utils.SPUtils;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ysxsoft.common_base.adapter.BaseQuickAdapter;
import com.ysxsoft.common_base.adapter.BaseViewHolder;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.frame.list.IListAdapter;
import com.ysxsoft.common_base.base.frame.list.ListManager;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.view.custom.image.RoundImageView;
import com.ysxsoft.common_base.view.widgets.MultipleStatusView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 排行榜
 */
@Route(path = "/main/RankingActivity")
public class RankingActivity extends BaseActivity implements IListAdapter<TopBean.DataBean.ListBean> {


    @BindView(R.id.tabs_pending)
    TabLayout tabsPending;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.multipleStatusView)
    MultipleStatusView multipleStatusView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;

    @BindView(R.id.rb1)
    RadioButton rb1;
    @BindView(R.id.rb2)
    RadioButton rb2;
    @BindView(R.id.rb3)
    RadioButton rb3;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.img_two)
    RoundImageView imgTwo;
    @BindView(R.id.text_two)
    TextView textTwo;
    @BindView(R.id.img_one)
    RoundImageView imgOne;
    @BindView(R.id.text_one)
    TextView textOne;
    @BindView(R.id.img_three)
    RoundImageView imgThree;
    @BindView(R.id.text_three)
    TextView textThree;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_num)
    TextView textNum;

    private List<String> mTitles_pending = new ArrayList<>();

    ListManager<TopBean.DataBean.ListBean> manager;
    private int type = 1;
    private int is_type = 1;

    public static void start() {
        ARouter.getInstance().build(ARouterPath.getRankingActivity())
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ranking;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void doWork() {
        super.doWork();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitles_pending.add("近7天");
        mTitles_pending.add("近30天");
        mTitles_pending.add("近90天");
        for (String title : mTitles_pending) {
            tabsPending.addTab(tabsPending.newTab().setText(title));
        }
        tabsPending.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (EmptyUtils.isNotEmpty(position)) {
                    switch (position) {
                        case 0:
                            type = 1;

                            request(1);
                            break;
                        case 1:
                            type = 2;

                            request(1);
                            break;
                        case 2:
                            type = 3;

                            request(1);
                            break;

                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        textNum.setText("场数");
                        is_type = 1;


                        request(1);
                        break;
                    case R.id.rb2:
                        textNum.setText("桌数");
                        is_type = 2;

                        request(1);
                        break;
                    case R.id.rb3:
                        textNum.setText("预付款");
                        is_type = 3;

                        request(1);
                        break;

                }
            }
        });
        rb1.setChecked(true);
        showView();
    }

    private void showView() {
        manager = new ListManager(this);
        manager.init(getWindow().findViewById(android.R.id.content));
        manager.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        manager.getAdapter().setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                request(manager.nextPage());
            }
        }, recyclerView);
        request(1);
    }

    @Override
    public int getItemLayoutId() {
        return R.layout.item_ranking_adepter;
    }
    int pages;
    @Override
    public void request(int page) {
        showLoadingDialog();
        String token = SPUtils.get(RankingActivity.this, "token", "").toString();
        OkHttpUtils.post()
                .url(Api.POST_TOP)
                .addHeader("flag", token)
                .addParams("type", type + "")
                .addParams("is_type", is_type + "")
                .addParams("page", "" + page)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
//                        Log.e("TAG", "排行榜onError   " + e.getMessage());
                        manager.releaseRefresh();
                        hideLoadingDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        manager.releaseRefresh();
                        hideLoadingDialog();
                        TopBean topBean = JsonUtils.parseByGson(response, TopBean.class);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(RankingActivity.this, "token", "");
                            Intent intent = new Intent(RankingActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        Log.e("TAG", "排行榜onResponse   " + response);

                        if (beanBean.getCode().equals("0")) {
                            if(page==1){
                                manager.resetPage();
                                if(topBean.getData().getList().size()>=3){
                                    Glide.with(RankingActivity.this).load(topBean.getData().getList().get(0).getAvatar()).into(imgOne);
                                    textOne.setText("" + topBean.getData().getList().get(0).getUsername());

                                    Glide.with(RankingActivity.this).load(topBean.getData().getList().get(1).getAvatar()).into(imgTwo);
                                    textTwo.setText("" + topBean.getData().getList().get(1).getUsername());

                                    Glide.with(RankingActivity.this).load(topBean.getData().getList().get(2).getAvatar()).into(imgThree);
                                    textThree.setText("" + topBean.getData().getList().get(2).getUsername());
                                }else if(topBean.getData().getList().size()==2){
                                    Glide.with(RankingActivity.this).load(topBean.getData().getList().get(0).getAvatar()).into(imgOne);
                                    textOne.setText("" + topBean.getData().getList().get(0).getUsername());

                                    Glide.with(RankingActivity.this).load(topBean.getData().getList().get(1).getAvatar()).into(imgTwo);
                                    textTwo.setText("" + topBean.getData().getList().get(1).getUsername());
                                }else if(topBean.getData().getList().size()==1){
                                    Glide.with(RankingActivity.this).load(topBean.getData().getList().get(0).getAvatar()).into(imgOne);
                                    textOne.setText("" + topBean.getData().getList().get(0).getUsername());
                                }
                            }
                            manager.setData(topBean.getData().getList());
                        }


                    }
                });
    }

    @Override
    public void fillView(BaseViewHolder helper, TopBean.DataBean.ListBean s) {
        String id = SPUtils.get(RankingActivity.this, "id", "").toString();
        Log.e("TAG", id);
        FrameLayout frame_gr = helper.itemView.findViewById(R.id.frame_gr);
        ImageView my_img = helper.itemView.findViewById(R.id.my_img);
        TextView text_top = helper.itemView.findViewById(R.id.text_top);
        RoundImageView img_avatar = helper.itemView.findViewById(R.id.img_avatar);
        TextView text_name = helper.itemView.findViewById(R.id.text_name);
        TextView text_num = helper.itemView.findViewById(R.id.text_num);

        if (id.equals("" + s.getId())) {
            frame_gr.setBackground(getResources().getDrawable(R.mipmap.icon_top_item_bg));
            my_img.setVisibility(View.VISIBLE);
        } else {
            frame_gr.setBackground(getResources().getDrawable(R.color.colorWhite));
            my_img.setVisibility(View.GONE);
        }
        int i = helper.getLayoutPosition() + 1;
        text_top.setText("" + i);
        Glide.with(RankingActivity.this).load(s.getAvatar()).into(img_avatar);
        text_name.setText("" + s.getUsername());
        text_num.setText("" + s.getNum());
    }

    @Override
    public void fillMuteView(BaseViewHolder helper, TopBean.DataBean.ListBean s) {

    }

    @Override
    public void attachActivity(AppCompatActivity activity) {

    }

    @Override
    public void dettachActivity() {

    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    @Override
    public boolean isMuteAdapter() {
        return false;
    }

    @Override
    public int[] getMuteTypes() {
        return new int[0];
    }

    @Override
    public int[] getMuteLayouts() {
        return new int[0];
    }
}